<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div{
            width: 100px;
            height: 100px;
            background-color: pink;
            margin: 10px auto;
        }
    </style>
</head>
<body>
<div class="demo1"></div>
<div></div>
<div></div>
<div id="demo"></div>
<div></div>
<div class="demo"></div>
<div></div>
<script>
    //获取id函数及
    function $(cla) {
        var s=cla.charAt(0);
        var ss=cla.substr(1);
        switch (s) {
            case "#":
                return document.getElementById(ss);
                break
            case ".":
                return document.getElementsByClassName(ss);
                break
            default:
                return document.getElementsByTagName(cla);
        }
    }
    for (var i=0;i<$(".demo").length;i++){
        $(".demo")[i].style.backgroundColor="red";
    }
</script>
</body>
</html>